package com.sinbader.xiaole.common.entity;

import cn.stylefeng.guns.sys.core.exception.StockException;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;

import com.baomidou.mybatisplus.annotation.Version;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;


@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("stock")
@ApiModel(value="Stock对象", description="库存")
public class Stock implements Serializable {

    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "商品")
    @TableField("product_id")
    private Long productId;

    @ApiModelProperty(value = "供应商")
    @TableField("supplier_id")
    private Long supplierId;

    @ApiModelProperty(value = "分店")
    @TableField("shop_id")
    private Long shopId;

    @ApiModelProperty(value = "仓库")
    @TableField("warehouse_id")
    private Long warehouseId;

    @ApiModelProperty(value = "货架")
    @TableField("shelf_Id")
    private Long shelfId;

    @ApiModelProperty(value = "销售可用库存")
    @TableField("sale_usable_count")
    private Integer saleUsableCount;

    @ApiModelProperty(value = "销售冻结库存")
    @TableField("sale_freeze_count")
    private Integer saleFreezeCount;

    @ApiModelProperty(value = "销售已用库存")
    @TableField("sale_used_count")
    private Integer saleUsedCount;

    @ApiModelProperty(value = "仓库可用库存")
    @TableField("warehouse_usable_count")
    private Integer warehouseUsableCount;

    @ApiModelProperty(value = "仓库冻结库存")
    @TableField("warehouse_freeze_count")
    private Integer warehouseFreezeCount;

    @ApiModelProperty(value = "仓库已用库存")
    @TableField("warehouse_used_count")
    private Integer warehouseUsedCount;

	@ApiModelProperty(value = "乐观锁")
	@TableField("version")
	@Version
	private Long version;

	@ApiModelProperty(value = "创建者")
	@TableField("creator_id")
	private Long creatorId;

	@ApiModelProperty(value = "创建时间")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    private Date updateTime;


    public void changeSaleUsableCount(int count) {
    	this.saleUsableCount += count;
	}
	public void changeSaleFreezeCount(int count) {
    	this.saleFreezeCount += count;
	}
	public void changeSaleUsedCount(int count) {
    	this.saleUsedCount += count;
	}

	public void changeWarehouseUsableCount(int count) {
    	this.warehouseUsableCount += count;
	}
	public void changeWarehouseFreezeCount(int count) {
    	this.warehouseFreezeCount += count;
	}
	public void changeWarehouseUsedCount(int count) {
    	this.warehouseUsedCount += count;
	}

    public Long nextVersion() {
    	version++;
    	return version;
	}

	public void checkCounts() {
    	boolean illegal = anyNullOrNegative(saleUsableCount, saleFreezeCount, saleUsedCount, warehouseUsableCount, warehouseFreezeCount, warehouseUsedCount);
    	if(illegal) {
    		throw new StockException();
		}
	}

	private boolean anyNullOrNegative(Integer... counts) {
    	for(Integer count : counts) {
    		if(count == null || count < 0) {
    			return true;
			}
		}
		return false;
	}

}
